//
//  LaserProjectile.m
//  iFly
//
//  Created by User-10 on 12/7/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "LaserProjectile.h"
#import "ProjectileFactory.h"
#import "VectorHelper.h"
#import "Constants.h"
#import "CharacterNode.h"

@implementation LaserProjectile

+(instancetype)laserWithPosition:(CGPoint)position andDamage:(NSInteger)damage {
    LaserProjectile* projectile = [super projectileWithImageNamed:@"laserbeam_blue.png" andPosition:position andDamage:damage];
    
    return projectile;
}

-(void)shoot {
    [super shoot];
    
    CGPoint target = CGPointMake(self.position.x+outOfScreenOffset, self.position.y);
    
    SKAction* move = [SKAction moveTo:target duration:1];
    SKAction* moveDone = [SKAction removeFromParent];
    [self runAction:[SKAction sequence:@[move, moveDone]]];
}

+(void) shootLaserFromPosition:(CGPoint) position atDirection:(CGPoint) laserDirection inScene:(SKScene*) scene {
    
    ProjectileNode* laser = [ProjectileFactory createProjectileWithType:ProjectileTypeLaser andPosition:position inScene:scene];
    [laser setScale:scene.size.height/sceneScaling*2];
    
    // Shooting the laser at the ship
    CGPoint location = laserDirection;
    // Determine offset of location to projectile
    CGPoint offset = [VectorHelper vectorSubPoint:location toPoint:position];
    
    // Get the direction of where to shoot
    CGPoint direction = [VectorHelper vectorNormalize:offset];
    
    // Make it shoot far enough to be guaranteed off screen
    CGPoint shootAmount = [VectorHelper vectorMultPoint:direction withMultiplier:outOfScreenOffset];
    
    // Add the shoot amount to the current position
    CGPoint realDest = [VectorHelper vectorAddPoint:shootAmount toPoint:laser.position];

    laser.zRotation = direction.y;
    
    SKAction* move = [SKAction moveTo:realDest duration:1];
    SKAction* moveDone = [SKAction removeFromParent];
    [laser runAction:[SKAction sequence:@[move,moveDone]]];
}

@end
